/**
 * hsjry.com Inc.
 * Copyright (c) 2014-2017 All Rights Reserved.
 */
package org.mybatis.generator.internal.util;

import static org.mybatis.generator.internal.util.messages.Messages.getString;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 扩展ClassloaderUtility
 * @author liangjf
 * @version $Id: ExtClassloaderUtility.java, v 1.0 2017年9月29日 下午8:08:10 liangjf Exp $
 */
public class ExtClassloaderUtility {

	/**
     * Utility Class - No Instances
     */
    private ExtClassloaderUtility() {
    }

    public static ClassLoader getCustomClassloader(List<String> entries) {
        List<URL> urls = new ArrayList<URL>();
        File file;

        if (entries != null) {
            for (String classPathEntry : entries) {
            	URL url = ClassLoader.getSystemResource(classPathEntry);
                file = new File(url.getPath());
                if (!file.exists()) {
                    throw new RuntimeException(getString(
                            "RuntimeError.9", classPathEntry)); //$NON-NLS-1$
                }
                urls.add(url);
            }
        }

        ClassLoader parent = Thread.currentThread().getContextClassLoader();

        URLClassLoader ucl = new URLClassLoader(urls.toArray(new URL[urls
                .size()]), parent);

        return ucl;
    }
}
